/**
 * Copyright (C) 2017-2018 pinganfang, Inc. All Rights Reserved.
 */
package com.pingan.haofang.searchcloud.solr.api;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.solr.client.solrj.beans.DocumentObjectBinder;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.pingan.haofang.searchcloud.api.IndexRowData;

/**
 * @author huhailiang937
 * @date 2017年5月5日下午2:07:26
 * @since 1.0.0
 */
public class DocumentIndexRowDataBinder extends DocumentObjectBinder {

    protected static Logger LOG = LoggerFactory.getLogger(DocumentIndexRowDataBinder.class);

    public List<String> toSolrDocumentIds(List<IndexRowData> rowDatas) {
        if (CollectionUtils.isEmpty(rowDatas)) {
            return Collections.emptyList();
        }
        List<String> ids = new ArrayList<String>(rowDatas.size());
        for (IndexRowData row : rowDatas) {
            try {
                Object id = row.getPkFieldValue();
                if (null != row.getPkFieldValue()) {
                    ids.add(id.toString());
                }
            } catch (Exception e) {
                LOG.error("toSolrDocumentIds has error:", e);
            }
        }
        return ids;
    }

    public List<SolrInputDocument> toSolrInputDocuments(List<IndexRowData> rowDatas) {
        if (CollectionUtils.isEmpty(rowDatas)) {
            return Collections.emptyList();
        }
        List<SolrInputDocument> documents = new ArrayList<SolrInputDocument>(rowDatas.size());
        for (IndexRowData row : rowDatas) {
            try {
                SolrInputDocument document = this.toSolrInputDocument(row);
                if (null != document) {
                    documents.add(document);
                }
            } catch (Exception e) {
                LOG.error("toSolrInputDocument has error:", e);
            }
        }
        return documents;
    }

    public SolrInputDocument toSolrInputDocument(IndexRowData rowData) {
        if (null == rowData) {
            return null;
        }
        SolrInputDocument document = new SolrInputDocument();
        for (Map.Entry<String, Object> entry : rowData.entrySet()) {
            
            document.addField(entry.getKey(), entry.getValue());
        }
        return document;
    }

    public IndexRowData toIndexRowData(SolrDocument solrDocument) {
        if (null == solrDocument) {
            return null;
        }
        IndexRowData rowData = new IndexRowData();
        for (String filedName : solrDocument.getFieldNames()) {
            rowData.put(filedName, solrDocument.getFieldValue(filedName));
        }
        return rowData;
    }

    public List<IndexRowData> toIndexRowData(SolrDocumentList solrDocumentList) {
        if (null == solrDocumentList) {
            return null;
        }
        List<IndexRowData> rowDatas = new ArrayList<IndexRowData>(solrDocumentList.size());
        for (int i = 0; i < solrDocumentList.size(); i++) {
            SolrDocument solrDocument = solrDocumentList.get(i);
            rowDatas.add(toIndexRowData(solrDocument));
        }
        return rowDatas;
    }

}
